package com.opencee.cloud.autoconfigure.context;

import com.opencee.common.security.Md5PasswordEncoder;
import com.opencee.cloud.autoconfigure.actuator.DbHealthIndicator;
import com.opencee.cloud.autoconfigure.actuator.RedisHealthIndicator;
import com.opencee.cloud.autoconfigure.utils.RestTemplateUtil;
import com.opencee.common.exception.DefaultRestResponseErrorHandler;
import com.opencee.common.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.client.RestTemplate;

/**
 * 配置类
 *
 * @author liuyadu
 */
@Slf4j
@Configuration
public class ContextAutoConfiguration implements ApplicationContextAware {

    ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 加密配置
     *
     * @return
     */
    @Bean
    @Primary
    @ConditionalOnMissingBean(PasswordEncoder.class)
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new Md5PasswordEncoder();
        log.info("密码加密类 [{}]", encoder);
        return encoder;
    }


    /**
     * Spring上下文工具配置
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SpringContextHolder.class)
    public SpringContextHolder springContextHolder() {
        SpringContextHolder holder = new SpringContextHolder();
        log.info("SpringContextHolder上下文工具类 [{}]", holder);
        return holder;
    }


    /**
     * 自定义Oauth2请求类
     *
     * @param
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(RestTemplateUtil.class)
    public RestTemplateUtil restfulUtil(RestTemplate restTemplate, LoadBalancerClient loadBalancerClient) {
        RestTemplateUtil util = new RestTemplateUtil(restTemplate, loadBalancerClient);
        //设置自定义ErrorHandler
        log.info("RestTemplateUtil接口请求类 [{}]", util);
        return util;
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        //设置自定义ErrorHandler
        restTemplate.setErrorHandler(new DefaultRestResponseErrorHandler());
        return restTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(DbHealthIndicator.class)
    public DbHealthIndicator dbHealthIndicator() {
        DbHealthIndicator dbHealthIndicator = new DbHealthIndicator();
        log.info("数据库心跳检测 [{}]", dbHealthIndicator);
        return dbHealthIndicator;
    }

    @Bean
    @ConditionalOnMissingBean(RedisHealthIndicator.class)
    public RedisHealthIndicator redisHealthIndicator() {
        RedisHealthIndicator redisHealthIndicator = new RedisHealthIndicator();
        log.info("Redis心跳检测 [{}]", redisHealthIndicator);
        return redisHealthIndicator;
    }
}
